package org.jeecg.modules.report.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jeecg.modules.report.mapper.DepartStoreMapper;
import org.jeecg.modules.report.service.IDepartStoreService;
import org.jeecg.modules.report.vo.departstore.CustomerOrderVO;
import org.jeecg.modules.report.vo.departstore.DepartStoreData;
import org.jeecg.modules.report.vo.departstore.Header;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DepartStoreServiceImpl implements IDepartStoreService {
	
	@Autowired
	ISysDepartService sysDepartService;
	
	@Autowired
	DepartStoreMapper departStoreMapper;
	

	@Override
	public List<Header> getHeaderList() {
		List<Header> list = new ArrayList<>();
		list.add(new Header("cus_code","客户代码"));
		list.add(new Header("cus_name","客户名称"));
		list.add(new Header("count","订单数量"));
		list.add(new Header("shipment_count","交货数量"));
		//查询除开顶级节点：安森珠宝之外的所有部门列表集合。
		List<SysDepart> departList = sysDepartService.getSubDepIdsByDepIdExcept("6d35e179cd814e3299bd588ea7daed3f");
		for(SysDepart sysDepart : departList) {
			list.add(new Header(sysDepart.getOrgCode(),sysDepart.getDepartName()));
		}
		return list;
	}

	/**
	 * 组装客户订单在各部门的存货数据列表
	 */
	@Override
	public List<Map<String, Object>> getCustomerOrderDepartStoreDataList() {
		List<CustomerOrderVO> customerOrderVOList = this.getCustomerOrderVOList();//客户订单集合
		Map<String,Object> departStoreDataList = this.getDepartStoreDataList(customerOrderVOList);//部门存货数据
		//查询除开顶级节点：安森珠宝之外的所有部门列表集合。
		List<SysDepart> departList = sysDepartService.getSubDepIdsByDepIdExcept("6d35e179cd814e3299bd588ea7daed3f");
		List<Map<String,Object>> mapList = new ArrayList<>();
		if(customerOrderVOList != null && departStoreDataList != null) {
			for(CustomerOrderVO vo : customerOrderVOList) {
				Map<String,Object> rowData = new HashMap<String,Object>();
				rowData.put("cusCode", vo.getCusCode());
				rowData.put("cusName", vo.getCusName());
				rowData.put("count", vo.getCount());
				rowData.put("shipmentCount", vo.getShipmentCount());
				if(departStoreDataList.containsKey(vo.getCusCode())) {
					List<DepartStoreData> cusDepartStoreDataList = (List)departStoreDataList.get(vo.getCusCode());
					int i = 1;
					for(SysDepart sysDepart : departList) {
						String tag = "depart"+i;
						String orgCode = sysDepart.getOrgCode();
						for(DepartStoreData departStoreData : cusDepartStoreDataList) {
							if(departStoreData.getOrgCode().equals(orgCode)) {
								rowData.put(tag, departStoreData.getCount());
							} else {
								rowData.put(tag, "");
							}
						}
						i++;
					}
				}
				mapList.add(rowData);
			}
		}
		return mapList;
	}

	
	/**
	 * 查询客户订单（状态是进入生产做货状态后的订单 status>=4）
	 */
	@Override
	public List<CustomerOrderVO> getCustomerOrderVOList() {
		return departStoreMapper.getCustomerOrderVOList();
	}
	
	/**
	 * 部门存货数据
	 */
	@Override
	public Map<String,Object> getDepartStoreDataList(List<CustomerOrderVO> customerOrderVOList) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<String> customerCodeList = this.getCustomerCodeList(customerOrderVOList);//客户代码列表
		if(customerCodeList !=null && customerCodeList.size()>0) {
			List<DepartStoreData> departStoreDataList = departStoreMapper.getDepartStoreDataList(customerCodeList);
			for(DepartStoreData data : departStoreDataList) {
				String cusCode = data.getCusCode();
				if(map.containsKey(cusCode)) {
					List<DepartStoreData> list = (List)map.get(cusCode);
					list.add(data);
				} else {
					List<DepartStoreData> list = new ArrayList<>();
					list.add(data);
					map.put(cusCode, list);
				}
			}
		}
		return map;
	}
	
	private List<String> getCustomerCodeList(List<CustomerOrderVO> customerOrderVOList) {
		List<String> customerCodeList = new ArrayList<>();
		for(CustomerOrderVO vo : customerOrderVOList) {
			customerCodeList.add(vo.getCusCode());
		}
		return customerCodeList;
	}
}
